package com.yifan.service;

import com.yifan.dao.BaseDao;
import com.yifan.entity.User;
import com.yifan.utils.JdbcUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserService {

	BaseDao dao = new BaseDao();

	// 获取列表
	public Map<String, Object> getUsers(String username, String realname, String sex,
			int page, int pageSize) {
		String sql = "select * from t_user where 1=1 ";
		String countsql = "select count(*) from t_user where 1=1 ";
		List<Object> list = new ArrayList<Object>();
		if (username != null && username != "") {
			sql += " and username = ? ";
			countsql += " and username = ? ";
			list.add(username);
		}
		if (realname != null && realname != "") {
			sql += " and realname = ? ";
			countsql += " and realname = ? ";
			list.add(realname);
		}
		if (sex != null && sex != "") {
			sql += " and sex = ? ";
			countsql += " and sex = ? ";
			list.add(sex);
		}

		int count = 0;
		List<Object> objects = null;
		try {
			count = dao.countObject(JdbcUtils.getconn(), countsql, list);

			sql += "  limit ?,?";
			list.add((page - 1) * pageSize);
			list.add(pageSize);

			objects = dao.getObjects(JdbcUtils.getconn(), sql, User.class, list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", getListUser(objects));
		map.put("count", count);
		map.put("msg", "用户列表");
		map.put("code", "0");
		return map;
	}

	// 添加
	public Map<String, String> addUser(User user) {
		Map<String, String> map = new HashMap<String, String>();
		String sql = "insert into t_user (username, password, realname, sex,  phone, userType) "
				+ "values (?,?,?,?,?,?)";
		User isuser = getUser(user.getUsername());
		if (isuser == null) {
			try {
				dao.addObject(JdbcUtils.getconn(), sql,
						new Object[] { user.getUsername(), user.getPassword(), user.getRealname(), user.getSex(),
								 user.getPhone(), user.getUserType() });
				map.put("msg", "ok");
				map.put("handle", "添加成功！");
			} catch (SQLException e) {
				e.printStackTrace();
				map.put("msg", "nok");
				map.put("handle", "发生错误了！");
			}
		} else {
			map.put("msg", "nok");
			map.put("handle", "该用户名已经存在！");
		}
		return map;
	}

	// 删除
	public Map<String, String> deleteUser(int id) {
		Map<String, String> map = new HashMap<String, String>();
		String sql = "delete from t_user where id=?";
		try {
			dao.deleteObject(JdbcUtils.getconn(), sql, new Object[] { id });
			map.put("msg", "ok");
			map.put("handle", "删除成功！");
		} catch (SQLException e) {
			e.printStackTrace();
			map.put("msg", "nok");
			map.put("handle", "发生错误了！");
		}
		return map;
	}

	// 批量删除
	public Map<String, String> deleteUsers(String[] idarr) {
		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < idarr.length; i++) {
			map = deleteUser(Integer.parseInt(idarr[i]));
		}
		return map;
	}

	// 修改用户
	public Map<String, String> updateUser(User user) {
		Map<String, String> map = new HashMap<String, String>();
		String sql = "update t_user set username=?, password=?, realname=?, sex=?,  phone=?, userType=? where id=?";
		User isuser = getUser(user.getUsername());
		if (isuser != null && user.getId() != isuser.getId()) {
			map.put("msg", "nok");
			map.put("handle", "该用户名已经存在！");
		} else {
			try {
				dao.updateObject(JdbcUtils.getconn(), sql,
						new Object[] { user.getUsername(), user.getPassword(), user.getRealname(), user.getSex(),
								user.getPhone(), user.getUserType(), user.getId() });
				map.put("msg", "ok");
				map.put("handle", "修改成功！");
			} catch (SQLException e) {
				e.printStackTrace();
				map.put("msg", "nok");
				map.put("handle", "发生错误了！");
			}
		}

		return map;
	}

	// 修改
	public Map<String, String> updateUserByU_P(String username, String password, String old_password,
			String new_password) {
		Map<String, String> map = new HashMap<String, String>();
		String sql = "update t_user set password=? where username=? and password=?";
		if (!password.equals(old_password)) {
			map.put("msg", "nok");
			map.put("handle", "旧密码不正确！");
		} else {
			try {
				dao.updateObject(JdbcUtils.getconn(), sql, new Object[] { new_password, username, password });
				map.put("msg", "ok");
				map.put("handle", "修改密码成功！");
			} catch (SQLException e) {
				e.printStackTrace();
				map.put("msg", "nok");
				map.put("handle", "发生错误了！");
			}
		}
		return map;
	}

	// 获取单个
	public User getUser(int id) {
		String sql = "select * from t_user where id=?";
		Object object = null;
		try {
			object = dao.getObject(JdbcUtils.getconn(), sql, User.class, new Object[] { id });
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return (User) object;
	}

	// 获取登录用户
	public User getUser(String username, String password) {
		String sql = "select * from t_user where username=? and password = ?";
		User user = null;
		try {
			user = (User) dao.getObject(JdbcUtils.getconn(), sql, User.class, new Object[] { username, password });
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	// 根据名字获取用户
	public User getUser(String username) {
		String sql = "select * from t_user where username=?";
		User user = null;
		try {
			user = (User) dao.getObject(JdbcUtils.getconn(), sql, User.class, new Object[] { username });
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return user;
	}

	public List<User> getListUser(List<Object> obj) {
		List<User> list = new ArrayList<User>();
		for (int i = 0; i < obj.size(); i++) {
			User user = (User) obj.get(i);
			list.add(user);
		}
		return list;
	}

}
